/*
 +--------------------------------------------------------------------------
 |   Mblog [#RELEASE_VERSION#]
 |   ========================================
 |   Copyright (c) 2014, 2015 mtons. All Rights Reserved
 |   http://www.mtons.com
 |
 +---------------------------------------------------------------------------
 */

define(function (require, exports, module) {
    J = jQuery;
    var player;

    function youkuPlayer(vid) {
        player = new YKU.Player('videoplayer', {
            styleid: '0',
            client_id: '7c068d0cb01cb88c',
            vid: vid
        });
    }

    function souhuPlayer(vid, code) {
        document.getElementById("videoplayer").innerHTML = createSHPlayer({
            bid: vid,
            autoplay: true,
            vars: {"enforceHTML5": true},
            width: "100%",
            height: "100%",
            getHTML: true,
            showCtrlBar: 0,
            channelSrc: 11510001,
            lqd: 16542,
            lcode: code
        });
    }

    $('input[name=url]').change(function () {
        var $input = $(this);
        var url = $input.val();
        var loader = $('<div class="video-loader"><i class="fa fa-spinner fa-spin"></i></div>');
        $input.parent().append(loader);
        jQuery.getJSON(app.base + '/video/take.json', {url: url}, function (ret) {
            loader.remove();
            if (ret.code >= 0) {
                $('#preview').show();
                if (ret.data.source == "优酷") {
                    youkuPlayer(ret.data.id);
                } else {
                    souhuPlayer(ret.data.id, ret.data.code);
                }
                setContent(ret.data.description);
            } else {
                layer.msg(ret.message, {icon: 2});
            }
        });
    });

});